package tree.leetcode_114_medium;

public class BestSolution {
    public void flatten(TreeNode root) {
        //base case
        if(root == null){
            return;
        }

        while(root != null){

            //保存节点的右孩子，将左孩子放置在右孩子处。
            TreeNode temp = root.right;
            root.right = root.left;
            //将左孩子置空
            root.left = null;

            //寻找最右节点，将保存的右孩子连上
            TreeNode cur = root;
            while(cur.right != null){
                cur = cur.right;
            }
            cur.right = temp;

            //对下个节点进行操作
            root = root.right;
        }
    }
}
